package cn.kduck;

import cn.kduck.security.UserExtInfo;
import cn.kduck.webapp.login.UserExtInfoImpl;
import cn.kduck.webapp.security.UserDetailsServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.security.core.userdetails.UserDetailsService;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import javax.mail.internet.MimeMessage;
import java.io.File;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Locale;

@SpringBootApplication
//@EnableRabbit
@EnableOpenApi
public class Application extends SpringBootServletInitializer {

//    @Autowired
//    private PasswordEncoder passwordEncoder;

    @Bean
    public UserDetailsService userDetailsService(){
        return new UserDetailsServiceImpl();
    }

//    @Bean
//    public AccountPwdEncoder accountPwdEncoder(PasswordEncoder passwordEncoder){
//        return new AccountPwdEncoder(){
//            @Override
//            public String encode(CharSequence rawPassword) {
//                return passwordEncoder.encode(rawPassword);
//            }
//
//            @Override
//            public boolean matches(CharSequence rawPassword, String encodedPassword) {
//                return passwordEncoder.matches(rawPassword,encodedPassword);
//            }
//        };
//    }

//    @Bean
//    public CustomRoleAccessVoter roleAccessVoter(AccountService accountService){
//        return new CustomRoleAccessVoter(accountService);
//    }

    @Bean
    public UserExtInfo userExtInfo(){
        return new UserExtInfoImpl();
    }


    public static void main(String[] args) throws Exception {

        InetAddress address = null;
        try {
            address = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        System.out.println(address.getHostAddress());

        ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);

//        JavaMailSenderImpl sender = new JavaMailSenderImpl();
//        sender.setHost("smtp.163.com");
//        sender.setUsername("liuhgmail@163.com");
//        sender.setPassword("SJMJJRLWKHXMXGGT");
//
//        MimeMessage message = sender.createMimeMessage();
//        // use the true flag to indicate you need a multipart message
//        MimeMessageHelper helper = new MimeMessageHelper(message, true);
//        message.setSubject("服务器信息梳理表同时附美图一张");
//        helper.setFrom("liuhgmail@163.com");
//        helper.setTo("lhg317@163.com");
////        helper.setCc();
////        helper.setBcc();
//
//    // use the true flag to indicate the text included is HTML
//        helper.setText("<html><body><img src='cid:lhg317'></body></html>", true);
//
//    // let's include the infamous windows Sample file (this time copied to c:/)
//        FileSystemResource res = new FileSystemResource(new File("D:\\User\\Pictures\\T.jpg"));
//        helper.addInline("lhg317", res);
//
//        helper.addAttachment("服务器信息梳理表.xlsx",new File("D:\\User\\Desktop\\服务器信息梳理表.xlsx"));
//        sender.send(message);

//        MessageSource messageSource = context.getBean(MessageSource.class);
//        System.out.println(messageSource.getMessage("birthday",new String[0], Locale.getDefault()));
//        System.out.println(messageSource.getMessage("AbstractUserDetailsAuthenticationProvider.badCredentials",new String[0], Locale.getDefault()));

//        System.out.println(messageSource.getMessage("name100",new String[0], Locale.getDefault()));


//        Enumeration<URL> urls = Application.class.getClassLoader().getResources(FACTORIES_RESOURCE_LOCATION);
//        while(urls.hasMoreElements()){
//            System.out.println(urls.nextElement());
//        }
    }

    @Bean
    public Docket allDocket() {
        return new Docket(DocumentationType.SWAGGER_2)//(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .paths(PathSelectors.ant("/**/*"))
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .build()
                .useDefaultResponseMessages(false);
    }


    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                //页面标题
                .title("K-Duck Demo")
                //创建人
                .contact(new Contact("LiuHG", "http://www.kduck.cn", ""))
                //版本号
                .version("1.0")
                //描述
                .description("API 描述")
                .build();
    }

}

